@include('backstage/public/_header')
    <style>
        .result_wrap dl{
            height: 76px;
            line-height: 76px;
        }
        .result_wrap dl dt{
            line-height: 76px;
        }
        .result_wrap form dt{
            font-weight: 700;
            float: left;
        }
        .result_wrap tr label{
            color:#333;
            font-weight: 700;
        }
        .add_tab .sub_tab{
            width: 100%;
            border-collapse: collapse;
            border-width:0px; 
            border-style:hidden;
        }
        table.add_tab tr td{
            text-align: left;
            font-size: 14px;
            padding:0;
        }
        input[type='checkbox']{margin-right: 0;}
        table.add_tab tr td label{margin-left: 10px;}
    </style>
    <!--結果集標題與導航組件 開始-->
    <div class="result_wrap">
        <!-- <div class="result_title">
            <h3>快捷操作</h3>
        </div>
        <div class="result_content">
            <div class="short_wrap">
                <a href="#"><i class="fa fa-plus"></i>新增文章</a>
                <a href="#"><i class="fa fa-recycle"></i>批量刪除</a>
                <a href="#"><i class="fa fa-refresh"></i>更新排序</a>
            </div>
        </div> -->
    </div>
    <!--結果集標題與導航組件 結束-->
    
    <div class="result_wrap">
        <form id="form_data" method="post">
        <input type="hidden" name="_token" value="{{csrf_token()}}">
        <input type="hidden" name="id" value="{{$info->id}}">
            <dl>
                <dt>用護組名稱：</dt>
                <dd><input type="text" name="role" maxlength="20" value="{{$info->role}}"></dd>
            </dl>
            <dl>
                <dt>用護組描述：</dt>
                <dd><textarea name="desc" style="resize:none;">{{$info->desc}}</textarea></dd>
            </dl>
            <table class="add_tab" border="1" cellspacing="0" cellpadding="0">
                @foreach ($data as $v)
                @if($v->pid==0)
                <tr>
                    <th style="color:red">{{$v->menu_name}}：</th>
                    <td width="170">
                        <span>選擇相關權限</span>
                        <label><input type="checkbox" data-id="{{$v->id}}" data-pid="{{$v->pid}}" >全選</label>
                    </td>
                    <td>
                        <table class="sub_tab" border="1" cellspacing="0" cellpadding="0">
                            @foreach ($data as $vv)
                            @if($v->id==$vv->pid)
                            <tr>
                                <th style="width:120px;color:#0c0;">{{$vv->menu_name}}：</th>
                                <td width="170">
                                    <span >選擇相關權限</span>
                                    <label ><input type="checkbox"  data-id="{{$vv->id}}" data-pid="{{$vv->pid}}" >全選</label>
                                </td>
                                <td>
                                    <table class="sub_tab" border="1" cellspacing="0" cellpadding="0">
                                    @foreach ($data as $val)
                                    @if($vv->id==$val->pid)
                                        <tr>
                                            <th style="width:120px;text-align: right;color: #333;">{{$val->menu_name}}：</th>
                                            <td>
                                                <input type="hidden" data-id="{{$val->id}}" data-pid="{{$val->pid}}" >
                                                @if(!empty($val->has_permissions))
                                                    @foreach ($val->has_permissions as $key=>$value)
                                                        <label><input type="checkbox" name="{{$val->action}}[]" data-pid="{{$val->id}}" value="{{$key}}" @if(!empty($info->authority[$val->action]) && in_array($key,$info->authority[$val->action])) checked="checked" @endif>{{$value}}</label>
                                                    @endforeach
                                                @endif
                                            </td>
                                        </tr>
                                    @endif
                                    @endforeach
                                    </table>
                                </td>
                            </tr>
                            @endif
                            @endforeach
                        </table>
                    </td>
                </tr>
                @endif
                @endforeach
            </table>
            <dl>
                <input type="button" value="提交" id="btnSub" onclick="onSubmit();">
                <input type="button" class="back" onclick="history.go(-1)" value="返回">
            </dl>
        </form>
        <div class="tips">
            <h3>修改用護組說明</h3>
            <p>1、選中的表示開通制定權限</p>
            <p>2、若某項權限下全部沒有選中，該組用護將看不到該菜單</p>
            <p>3、請根據實際需求勾選權限和去除權限</p>
            <p>4、點擊提交 -&gt; 修改用護組，返回-&gt; 可以回到上壹層頁面</p>
        </div>
    </div>
    <script type="text/javascript">
        $("input[type='checkbox']").click(function(){
            if(!$(this).attr("data-id")) return ;//如果被點擊的元素不是父元素就不執行
            var id = $(this).attr('data-id');
            if($(this).prop("checked")){
                $("input[data-pid='"+id+"']").prop("checked",'true');
            }else{
                $("input[data-pid='"+id+"']").removeProp("checked");
            }
            che($("input[data-pid='"+id+"']"));
        });
        function che(obj){
            if(obj.length==0) return;
            if(obj.length>0){
                for(var i=0;i<obj.length;i++){
                    var id = obj.eq(i).attr('data-id');
                    // var che = $("input[data-id='"+id+"']").is(":checked");
                    // console.log($("input[data-id='"+id+"']"));
                    if(obj.eq(i).prop("checked")){
                        $("input[data-pid='"+id+"']").prop("checked",'true');
                    }else{
                        $("input[data-pid='"+id+"']").removeProp("checked");
                    }
                    che($("input[data-pid='"+id+"']"));
                }
            }
        } 
        var vfTime = 2;
        var timer=null;
        function setTime(){
            vfTime --;
            if(vfTime<=0){
                vfTime = 0;
                clearInterval(timer);
            }
        }

        function onSubmit(){
            var role=$(":input[name='role']").val();
            if (vfTime!=0 && timer!=null){
                layer.msg('操作過於頻繁', {icon: 7,anim: 6});
                // setTimeout(function(){$("#chiContent").focus();}, 1500);
                return false;
            }
            if (role==''){
                layer.msg('用護組名稱不能為空', {icon: 5,anim: 6});
                setTimeout(function(){$(":input[name='role']").focus();}, 0);
                return false;
            }
            
            var form = new FormData(document.getElementById("form_data"));
            $.ajax({
                url:"{{$doedit}}",
                type:"post",
                data:form,
                processData:false,
                contentType:false,
                dataType : "json",
                success:function(data){
                    if(data.status==1){
                        layer.msg(data.message, {icon: 6});
                        setTimeout(function(){
                            window.location.href=data.jumpUrl;
                        },2000);
                    }else if(data.status==-1){
                        layer.alert(data.message, {icon: 2,anim:6});
                    }else{
                        layer.msg(data.message, {icon: 5});
                    }
                    clearInterval(timer);
                    timer = setInterval(setTime, 1000, "1");
                },
                error:function(e){
                    layer.msg(e, {icon: 5,anim: 6});
                    clearInterval(timer);
                    timer = setInterval(setTime, 1000, "1");
                }
            });
        }
    </script>
@include('backstage/public/_footer')